import Player from 'xgplayer'
import Music from '@/Components/Music/build/';
import api from 'Plugins/api'
import util from '@/utils/util';

const chnage = Music.prototype.change;

const extendsFuncs = {
    origenChnage: chnage,
    remove(url, playItem) {
        let idx = -1
        this.list.every((item, index) => {
            if (item.src === url || item.name === url || item.vid === url) {
                idx = index
                return false
            } else {
                return true
            }
        })
        if (idx > -1) {
            playItem ? this.list.splice(idx, 1, playItem) : this.list.splice(idx, 1)
        }
    },
    change() {
        let self = this;
        const togglePlayFunc = this.config.togglePlayFunc && this.config.togglePlayFunc(self.list[self.index], self.index);
        
        if (util.isPromise(togglePlayFunc)){
            togglePlayFunc.then(result => {
                this.origenChnage();
            })
        }else{
            this.origenChnage();
        };
    }
}

for (const key in extendsFuncs) {
    Music.prototype[key] = extendsFuncs[key];
};

export default Music;